
#include "zwmk.h"
#include "oled.h"

UART_HandleTypeDef UART1_Handler;
UART_HandleTypeDef UART2_Handler;

uint8_t ZWMK_Buf[BufSize]={0};
uint8_t Temp[BufSize]={0};
uint8_t con=0;
uint8_t f=0;
uint8_t RxCounter=0;
uint8_t Addr_Head[7]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01};
uint8_t GetImage[]={0x00,0x03,0x01,0x00,0x05,'>'};
uint8_t GetImageAck[]={0x07,0x00,0x03,0x00,'>'};
uint8_t Search[]={0x00,0x08,0x04,0x01,0x00,0x00,0x00,0x14,0x00,0x22,'>'};
uint8_t Identify[]={0x00,0x03,0x11,0x00,0x15,'>'};
uint8_t Match[]={0x00,0x03,0x03,0x00,0x07,'>'};

uint8_t MatchAck[]={0x07,0x00,0x05,0x00,'>'};
uint8_t IdentifyAck[]={0x07,0x00,0x07,0x00,'>'};
uint8_t GenChar[]={0x00,0x04,0x02,0x01,0x00,0x08,'>'};
uint8_t GenCharAck[]={0x07,0x00,0x03,0x00,'>'};
uint8_t SearchAck[]={0x07,0x00,0x07,0x00,'>'};

uint8_t flag=0;
uint8_t waittime=5;

void ZWMK_Init()
{

   UART_Init();

   ZWMK_GPIO_Init();





}


void ZWMK_GPIO_Init(){


     ZWMK_Wake_ON;
	
	   GPIO_InitTypeDef GPIO_Initer;
     
	   GPIO_Initer.Pin=ZWMK_Wake_Pin;
	   GPIO_Initer.Mode=GPIO_MODE_IT_RISING;
	   GPIO_Initer.Speed=GPIO_SPEED_HIGH;
     GPIO_Initer.Pull=GPIO_PULLDOWN;

     HAL_GPIO_Init(ZWMK_Wake_Port,&GPIO_Initer);
	
	   HAL_NVIC_SetPriority(EXTI9_5_IRQn,2,0);
     HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
	

    
}



void ZWMK_SData()
{
	    
      	
    
	
	


}

uint8_t ZWMK_SCmd(uint8_t* cmd,uint8_t* ack,uint16_t time)
{ 
     uint8_t len=0,waittime=3;
	   
	   len=DataLen(cmd);
		 
		memset(Temp,0,BufSize);
		memset(ZWMK_Buf,0,BufSize);
		RxCounter=0;
		 
	do{	 
		
		memset(ZWMK_Buf,0,BufSize);
		

	 HAL_UART_Transmit(&UART1_Handler,Addr_Head,7,0xff);   // 

	 HAL_UART_Transmit(&UART1_Handler,cmd,len,0xff);   //
	 
	 HAL_Delay(time);
		
//		OLED_Num(ZWMK_Buf,0,0);
		
		memset(Temp,0,BufSize);
		RxCounter=0;
		
	}while((memcmp(ack,ZWMK_Buf,DataLen(ack))!=0)&&--waittime);
	
	 
  return waittime;

}

uint8_t ZWMK_Check(uint8_t* ack,const uint8_t* temp)
{
     uint8_t count=2;
 
    
		
		
	  while((memcmp(ack,ZWMK_Buf,DataLen(ack))!=0)&&count--);
				
				if(count){
				
					return 0;
				
				}

			return 1;

}



uint8_t DataLen(uint8_t* data)
{
     uint8_t len=0;
	
	   while(*data!='>'){
		 
			 len++;
		 
			 data++;
			 
		 }


  return len;
}




void UART_Init(){

    

//   __HAL_RCC_UART1_CLK_ENABLE();
	
	 
   
	 UART1_Handler.Instance=USART1;
   UART1_Handler.Init.BaudRate=115200;
	 UART1_Handler.Init.Mode=UART_MODE_TX_RX;
	 UART1_Handler.Init.WordLength=UART_WORDLENGTH_8B;
	 UART1_Handler.Init.StopBits=UART_STOPBITS_1;
	 UART1_Handler.Init.Parity=UART_PARITY_NONE;
	 UART1_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
	 UART1_Handler.Init.OverSampling = UART_OVERSAMPLING_16;
	
	 HAL_UART_Init(&UART1_Handler);
	
	 __HAL_UART_ENABLE(&UART1_Handler);
	
	__HAL_UART_ENABLE_IT(&UART1_Handler,UART_IT_RXNE);
	__HAL_UART_ENABLE_IT(&UART1_Handler,UART_IT_IDLE);
	
	
//   HAL_UART_Receive_IT(&UART1_Handler,&con,1);

   UART2_Handler.Instance=USART2;
   UART2_Handler.Init.BaudRate=115200;
	 UART2_Handler.Init.Mode=UART_MODE_TX_RX;
	 UART2_Handler.Init.WordLength=UART_WORDLENGTH_8B;
	 UART2_Handler.Init.StopBits=UART_STOPBITS_1;
	 UART2_Handler.Init.Parity=UART_PARITY_NONE;
	 UART2_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
	 UART2_Handler.Init.OverSampling = UART_OVERSAMPLING_16;
	
//	 HAL_UART_Init(&UART2_Handler);
	
//	 __HAL_UART_ENABLE(&UART2_Handler);
	
//	__HAL_UART_ENABLE_IT(&UART2_Handler,UART_IT_RXNE);
	
	
	
		
		
	
	
	

}


void USART1_IRQHandler(void)// 
{
      
	  


	   if(__HAL_UART_GET_IT_SOURCE(&UART1_Handler,UART_IT_RXNE)!=RESET){		
	
			   uint8_t ch=0;
			 
		     Temp[RxCounter]=(uint8_t)USART1->DR;
//			   memcpy(ZWMK_Buf,Temp,RxCounter+1);
			  
			 HAL_UART_Transmit(&UART2_Handler,&Temp[RxCounter],1,0xff);
			 
			   RxCounter++;
			 
			 
			 if(RxCounter>BufSize-1){
			 
				 RxCounter=0;
				 memset(Temp,0,BufSize);
	
			 
			 }

		 }
		 
		 
	 if(__HAL_UART_GET_IT_SOURCE(&UART1_Handler,UART_IT_IDLE)!=RESET){
		 
			  uint8_t temp=0;
			          temp=USART1->SR;
			          temp=USART1->DR;
		 
		
	
			 memcpy(ZWMK_Buf,Temp+6,4);
		  
//		   HAL_UART_Transmit(&UART2_Handler,Temp,RxCounter,0xff);
//		   memset(Temp,0,RxCounter);
//		   RxCounter=0;
		 
		  
		 
		  flag=1;
		 
		
		 
		 }

}




void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{



}



void EXTI9_5_IRQHandler()
{

      if(__HAL_GPIO_EXTI_GET_IT(ZWMK_Wake_Pin)!=RESET){
			
				   __HAL_GPIO_EXTI_CLEAR_IT(ZWMK_Wake_Pin);
				
//				   HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,0);
				
				 
				 f=1;
			
			
			}




}




int fputc(int ch,FILE *f)
{
    uint8_t temp[1]={ch};
    HAL_UART_Transmit(&UART2_Handler,temp,1,0xffff);        //UartHandle??????
}




